package com.anjian.bamin.anjiandianzimap.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.aiton.administrator.shane_library.shane.ZjbBaseActivity;
import com.aiton.administrator.shane_library.shane.utils.ACache;
import com.aiton.administrator.shane_library.shane.utils.PinyinUtils;
import com.anjian.bamin.anjiandianzimap.R;
import com.anjian.bamin.anjiandianzimap.adapter.MySearchAdapter;
import com.anjian.bamin.anjiandianzimap.contant.Constant;
import com.anjian.bamin.anjiandianzimap.model.CompanyInfo;
import com.anjian.bamin.anjiandianzimap.model.CompanyList;
import com.anjian.bamin.anjiandianzimap.utils.MyComparator;
import com.blankj.utilcode.utils.RegexUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SearchActivity extends ZjbBaseActivity implements View.OnClickListener {

    private EditText mEditTextSearch;
    private ListView mListSearch;
    private List<CompanyInfo> mCompanyInfoList = new ArrayList<>();
    private List<CompanyInfo> mCompanyInfoListSearch = new ArrayList<>();
    private MySearchAdapter mMySearchAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void findID() {
        mEditTextSearch = (EditText) findViewById(R.id.editTextSearch);
        mListSearch = (ListView) findViewById(R.id.listSearch);
    }

    @Override
    protected void initViews() {
        mMySearchAdapter = new MySearchAdapter(this, mCompanyInfoListSearch);
        mListSearch.setAdapter(mMySearchAdapter);


    }

    @Override
    protected void setListeners() {
        mEditTextSearch.addTextChangedListener(new MyTextWatcher());
        findViewById(R.id.imgBack).setOnClickListener(this);
        mMySearchAdapter.setOnCompanyClickListener(new MySearchAdapter.OnCompanyClickListener() {
            @Override
            public void onCompanyClick(CompanyInfo companyInfo) {
                Intent intent = CompanyDetialActivity.newIntent(SearchActivity.this, companyInfo,true);
                startActivityTo(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mCompanyInfoList.clear();
        ACache aCacheCompanyList = ACache.get(SearchActivity.this, Constant.ACACHE.COMPANY_LIST_ACACHE);
        CompanyList companyList = (CompanyList) aCacheCompanyList.getAsObject(Constant.ACACHE.COMPANY_LIST);
        List<CompanyInfo> companyInfoList = companyList.getCompanyInfoList();
        mCompanyInfoList.addAll(companyInfoList);
        Collections.sort(mCompanyInfoList,new MyComparator());
        mMySearchAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imgBack:
                finishTo();
                break;
        }
    }

    class MyTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            mCompanyInfoListSearch.clear();
            if (s.length()==0){

            }else{
                if(RegexUtils.isChz(s.toString())){//判断汉字
                    for (int i = 0; i < mCompanyInfoList.size(); i++) {
                        CompanyInfo companyInfo = mCompanyInfoList.get(i);
                        String companyName = companyInfo.getCompanyName();
                        String alpha = PinyinUtils.getAlpha(companyName);
                        Log.e("MyTextWatcher", "onTextChanged: --->>首字母" +alpha );

                        if (companyInfo.getCompanyName().contains(s)){
                            mCompanyInfoListSearch.add(companyInfo);
                        }
                    }
                }else{
                    String sUp = s.toString().toUpperCase();
                    for (int i = 0; i < mCompanyInfoList.size(); i++) {
                        CompanyInfo companyInfo = mCompanyInfoList.get(i);
                        if (companyInfo.getCompanyPinyin().contains(sUp)){
                            mCompanyInfoListSearch.add(companyInfo);
                        }
                    }

                }


            }
            mMySearchAdapter.notifyDataSetChanged();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
    
//     class MyAdapter extends BaseAdapter {
//
//             @Override
//             public int getCount() {
//                 return mCompanyInfoListSearch.size();
//             }
//
//             @Override
//             public Object getItem(int position) {
//                 return null;
//             }
//
//             @Override
//             public long getItemId(int position) {
//                 return 0;
//             }
//
//             @Override
//             public View getView(int position, View convertView, ViewGroup parent) {
//                 View inflate = getLayoutInflater().inflate(R.layout.item_search, null);
//                 TextView textView_companyName =  (TextView) inflate.findViewById(R.id.textView_companyName);
//                 TextView textView_Address = (TextView) inflate.findViewById(R.id.textView_Address);
//                 textView_companyName.setText(mCompanyInfoListSearch.get(position).getCompanyName());
//                 Log.e("getView ", "getView 搜索结果"+mCompanyInfoListSearch.get(position).getCompanyName());
//                 textView_Address.setText(mCompanyInfoListSearch.get(position).getCompanyAddress());
//                 return inflate;
//             }
//         }

}
